// 通用方法集合
import CommonDefine from "@/component/CommonDefine";

const appStore = CommonDefine.appStore();

const decribeVisitGroup = (vg) => {
  let r = "";
  if (vg.groupType.T == "VgtPubKey") {
    r += "通过用户公钥访问";
  } else if (vg.groupType.T == "VgtAnonymous") {
    r += "匿名访问";
  } else if (vg.groupType.T == "VgtCode") {
    r += "访问码:" + vg.groupType.C.k + " 校验码:" + vg.groupType.C.c;
  }
  r += " / ";
  if (vg.ruleM) {
    r += " 模型访问规则 " + vg.ruleM.length + " 条";
  }
  if (vg.ruleF) {
    r += " 字段访问规则 " + vg.ruleF.length + " 条";
  }
  if (vg.ruleFv) {
    r += " 记录访问规则 " + vg.ruleFv.length + " 条";
  }
  return r;
};

const getUserName = (userId) => {
  if (userId == localStorage.getItem("uid")) {
    return "自己";
  } else {
    return appStore.otherUsers.find((u) => u.id == userId).name;
  }
};

const describeShareRuleModel = (rules) => {
  let r = "";
  rules.forEach((x) => {
    r += appStore.arySrmType.find((y) => y.key == x.T).label + " ";
    if (x.T != "SrmRecMax") {
      let offSet = 0;
      if (x.T == "VrVisitor") {
        r += " 其他贡献者 '" + getUserName(x.C[0]) + "'";
        offSet = 1;
      }
      if (x.C[offSet]) {
        r += " 可见";
      }
      if (x.C[offSet + 1]) {
        r += " 可修改";
      }
      if (x.C[offSet + 2]) {
        r += " 可删除";
      }
    } else {
      r += x.C + "条";
    }
    r += "；";
  });
  return r;
};

// 将记录的定制属性转换成一个字符串
const describeRecord = (record) => {
  let r_str = "";
  for (const attr in record) {
    if (parseInt(attr) > 100) {
      r_str += " | " + record[attr];
    }
  }
  return r_str.substring(3);
};

// 生成可读性更高的文件大小描述
const file_size_label = (sizeByte) => {
  if (sizeByte < 1024) {
    return sizeByte + " B";
  } else if (sizeByte < 1024 * 1024) {
    return (sizeByte / 1024).toFixed(1) + " KB";
  } else if (sizeByte < 1024 * 1024 * 1024) {
    return (sizeByte / (1024 * 1024)).toFixed(1) + " MB";
  } else {
    return (sizeByte / (1024 * 1024 * 1024)).toFixed(1) + " GB";
  }
};

export default {
  decribeVisitGroup,
  getUserName,
  describeShareRuleModel,
  describeRecord,
  file_size_label,
};
